/*
 * Javascript Agile Suite - JasMock Library
 * http://code.google.com/p/jasproject/
 *
 * Copyright (C) 2009 Luke Schafer
 * Licensed under MIT X11 license:
 * http://www.opensource.org/licenses/mit-license.php
 */

var JasMock = (function()
{
	function _jasMock() { }

	_jasMock.prototype.create = function(config)
	{
		var mock = {};
		for (var i in config)
		{
			if (config.hasOwnProperty(i))
			{
				mock[i] = (!Jas.isFunction(config[i])) ? config[i] : wrap(mock, config[i]);
			}
		}
		return mock;
	};

	_jasMock.prototype.from = function(type, stubs)
	{
		var mock = {};
		stubs = stubs || {};

		for (var i in type.prototype)
		{
			mock[i] = (!Jas.isFunction(type.prototype[i])) ? type.prototype[i] : wrap(mock, stubs[i] || type.prototype[i]);
		}
		return mock;
	};

	_jasMock.prototype.wrap = function(obj, stubs)
	{
		stubs = stubs || {};

		for (var i in obj.prototype)
		{
			if (Jas.isFunction(obj.prototype[i])) obj.prototype[i] = wrap(obj.prototype, stubs[i] || obj.prototype[i]);
		}
		return obj;
	};

	function wrap(scope, func)
	{
		var called = false;
		var tookArgs = [];
		var returnVal;

		function wrapper()
		{
			called = true;
			tookArgs = arguments;
			returnVal = func.apply(scope, arguments);
			return returnVal;
		}

		wrapper.checkWasCalled = function()
		{
			return called;
		};

		wrapper.checkTookArgs = function()
		{
			if (arguments.length != tookArgs.length) return false;

			for (var i = 0; i < arguments.length; i++)
			{
				if (tookArgs[i] != arguments[i]) return false;
			}
			return true;
		};

		wrapper.checkReturned = function(val)
		{
			return (val == returnVal);
		};

		return wrapper;
	}

	return new _jasMock();
})();